package com.sh.data.engine.common.util;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: mengzheng.mhc
 * @date: 2024/6/11 15:32
 */
public class RedisKeyUtil {

    public static String getTableSizeKeyByTableName(String projectId, String tableName) {
        return upperCase("table_size_" + projectId + "_" + tableName);
    }

    public static List<String> getTableSizeKeyListByTableName(
        String projectId, Collection<String> tableNameColl) {
        if (CollectionUtils.isEmpty(tableNameColl)) {
            return Collections.emptyList();
        }
        return tableNameColl.stream()
            .distinct()
            .map(e -> getTableSizeKeyByTableName(projectId, e))
            .collect(Collectors.toList());
    }

    public static String getProjectSizeKeyByProjectId(Long projectId) {
        return upperCase("project_size_" + projectId);
    }

    public static String getActiveProjectKeyByProjectId(Long projectId) {
        return upperCase("active_project_" + projectId);
    }

    public static String getMainDataQueryIndexName(String indexName) {
        return "main_data_" + indexName;
    }

    private static String upperCase(String key) {
        if (StringUtils.isBlank(key)) {
            return null;
        }
        return StringUtils.upperCase(key);
    }

    public static String getProjectOfflineTaskCountKeyByProjectId(String projectId) {
        return upperCase("project_offline_task_count_" + projectId);
    }

    public static String getProjectOfflineExpTaskCountKeyByProjectId(String projectId) {
        return upperCase("project_offline_exp_task_count_" + projectId);
    }

    public static String getDictMappingDetailKeyByProjectId(Long projectId) {
        return upperCase("dict_mapping_detail_ext_" + projectId);
    }

    public static String getDictMappingReportKeyByProjectId(Long projectId) {
        return upperCase("dict_mapping_report_ext_" + projectId);
    }

    public static String getKafkaTopicCacheKeyByDsLink(String dsLink) {
        return upperCase("kafka_topic_" + dsLink);
    }

    public static String getProjectFailTaskCountKeyByProjectId(String projectId) {
        return upperCase("project_fail_task_count_" + projectId);
    }

    public static String getProjectTaskCountKeyByProjectId(String projectId) {
        return upperCase("project_task_count_" + projectId);
    }

    public static String getProjectSubmitTaskCountKeyByProjectId(String projectId) {
        return upperCase("project_submit_task_count_" + projectId);
    }

    public static String getFlowCodeKey(Long modelId, Integer index) {
        return upperCase("md_flow_code:" + modelId + ":" + index);
    }

    public static String getApprovalKey(String approvalId) {
        return upperCase("md_approval:" + approvalId);
    }

    public static String getPrePublishVersionKey(Long metaModelId) {
        return upperCase("md_pre_publish:" + metaModelId);
    }
}
